package com.kfm.wiki.config;

import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.kfm.wiki.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class SpringConfig implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;

    @Bean
    public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder(){
        // json 转换时， 把 long 转换为 string
        Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder = new Jackson2ObjectMapperBuilder();
        return jackson2ObjectMapperBuilder.serializerByType(Long.class, ToStringSerializer.instance)
                .serializerByType(Long.TYPE, ToStringSerializer.instance);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/login")
                .excludePathPatterns("/category/all")
                .excludePathPatterns("/ebook/list")
                .excludePathPatterns("/doc/all")
                .excludePathPatterns("/content/*")
                .excludePathPatterns("/doc/vote/*")
                .excludePathPatterns("/snapshot/**")
                .excludePathPatterns("/test/**");
    }
}
